home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / Gimp / Util.pm < prev   
Encoding:
Perl POD Document  |  2003-01-14  |  9.7 KB  |  354 lines

  1. =head1 NAME
  2.  
  3.  Gimp::Util - some handy routines for Gimp-Perl users
  4.  
  5. =head1 SYNOPSIS
  6.  
  7.  use Gimp;
  8.  use Gimp::Util;
  9.  
  10. =head1 DESCRIPTION
  11.  
  12. Gimp-Perl is nice, but when you have to write everytime 10 lines just to
  13. get some simple functions done, it very quickly becomes tedious :-/
  14.  
  15. This module tries to define some functions that aim to automate frequently
  16. used tasks, i.e. its a sort of catch-all-bag for (possibly) useful macro
  17. functions.  If you want to add a function just mail the author of the
  18. Gimp-Perl extension (see below).
  19.  
  20. In Gimp-Perl (but not in Gimp as seen by the enduser) it is possible to
  21. have layers that are NOT attached to an image. This is, IMHO a bad idea,
  22. you end up with them and the user cannot see them or delete them. So we
  23. always attach our created layers to an image here, too avoid memory leaks
  24. and debugging times.
  25.  
  26. These functions try to preserve the current settings like colors, but not
  27. all do.
  28.  
  29. These functions can also be handled in exactly the same way as
  30. PDB-Functions, i.e. the (hypothetical) function C<gimp_image_xyzzy> can be
  31. called as $image->xyzzy, if the module is available.
  32.  
  33. The need to explicitly C<use Gimp::Util> will go away in the future.
  34.  
  35. =head1 FUNCTIONS
  36.  
  37. =over 4
  38.  
  39. =cut
  40.  
  41. package      Gimp::Util;
  42. require      Exporter;
  43. @ISA       = qw(Exporter);
  44. @EXPORT    = qw(
  45.                 layer_create 
  46.                 text_draw 
  47.                 image_create_text
  48.                 layer_add_layer_as_mask
  49.                );
  50. #@EXPORT_OK = qw();
  51.  
  52. use Gimp;
  53.  
  54. $VERSION=1.211;
  55.  
  56. ##############################################################################
  57. =pod
  58.  
  59. =item C<get_state ()>, C<set_state state>
  60.  
  61. C<get_state> returns a scalar representing most of gimps global state
  62. (at the moment foreground colour, background colour, active gradient,
  63. pattern and brush). The state can later be restored by a call to
  64. C<set_state>. This is ideal for library functions such as the ones used
  65. here, at least when it includes more state in the future.
  66.  
  67. =cut
  68.  
  69. sub get_state() {
  70.    [
  71.     Palette->get_foreground,
  72.     Palette->get_background,
  73.     Gradients->get_active,
  74.     scalar Patterns->get_pattern,
  75.     scalar Brushes->get_brush,
  76.    ]
  77. }
  78.  
  79. sub set_state($) {
  80.    my $s = shift;
  81.    Palette->set_foreground($s->[0]);
  82.    Palette->set_background($s->[1]);
  83.    Gradients->set_active($s->[2]);
  84.    Patterns->set_pattern($s->[3]);
  85.    Brushes->set_brush($s->[4]);
  86. }
  87.  
  88. ##############################################################################
  89. =pod
  90.  
  91. =item C<layer_create image,name,color,pos>
  92.  
  93. create a colored layer, insert into image and return layer
  94.  
  95. =cut
  96.  
  97. # [12/23/98] v0.0.1 Tels - First version
  98. sub layer_create {
  99.   my ($image,$name,$color,$pos) = @_;
  100.   my $layer;
  101.   my $tcol; # scratch color
  102.  
  103.   # create a colored layer
  104.   $layer = Gimp->layer_new ($image,Gimp->image_width($image),
  105.                            Gimp->image_height($image),
  106.                            RGB_IMAGE,$name,100,NORMAL_MODE);
  107.   $tcol = Gimp->palette_get_background ();
  108.   Gimp->palette_set_background ($color);
  109.   Gimp->drawable_fill ($layer,BG_IMAGE_FILL);
  110.   Gimp->image_add_layer($image, $layer, $pos);
  111.   Gimp->palette_set_background ($tcol); # reset
  112.   $layer;  
  113.   }
  114.  
  115. ##############################################################################
  116. =pod
  117.  
  118. =item C<text_draw image,layer,text,font,size,fgcolor>
  119.  
  120. Create a colored text, draw over a background, add to img, ret img.
  121.  
  122. =cut
  123.  
  124. # [12/23/98] v0.0.1 Tels - First version
  125. sub text_draw {
  126.   my ($image,$layer,$text,$font,$size,$fgcolor) = @_;
  127.   my ($bg_layer,$text_layer);
  128.   my $tcol; # temp. color
  129.  
  130.   warn __"text string is empty" if $text eq "";
  131.   warn __"no font specified, using default" if $font eq "";
  132.   $font = "Helvetica" if ($font eq "");
  133.  
  134.   $tcol = Gimp->palette_get_foreground ();
  135.   Gimp->palette_set_foreground ($fgcolor);
  136.   # Create a layer for the text.
  137.   $text_layer = Gimp->text($image,-1,0,0,$text,10,1,$size,
  138.                 PIXELS,"*",$font,"*","*","*","*"); 
  139.     
  140.   # Do the fun stuff with the text.
  141.   Gimp->layer_set_preserve_trans($text_layer, FALSE);
  142.  
  143.   if ($resize == 0)
  144.     {
  145.     # Now figure out the size of $image
  146.     $width = Gimp->image_width($text_layer);
  147.     $height = Gimp->image_height($text_layer);
  148.     # and cut text layer
  149.     }
  150.   else
  151.     {
  152.     }
  153.                       
  154.   # add text to image
  155.   Gimp->image_add_layer($image, $text_layer, $pos);
  156.   # merge white and text
  157.   Gimp->image_merge_visible_layers ($image,1);
  158.   # cleanup the left over layer (!) 
  159.   Gimp->layer_delete($text_layer);
  160.   $layer;
  161. }
  162.     
  163. ##############################################################################
  164. =pod
  165.  
  166. =item C<image_create_text text,font,size,fgcolor,bgcolor>
  167.  
  168. Create an image, add colored text layer on a background layer, return img.
  169.  
  170. =cut
  171.  
  172. # [12/23/98] v0.0.1 Tels - First version
  173. sub image_create_text {
  174.   my ($text,$font,$size,$fgcolor,$bgcolor) = @_;
  175.   my $tcol; # temp. color
  176.   my $text_layer;
  177.   my $bg_layer;
  178.   my $image;
  179.  
  180.   warn (__"text string is empty") if ($text eq "");
  181.   warn (__"no font specified, using default") if ($font eq "");
  182.   $font = "Helvetica" if ($font eq "");
  183.   # create an image. We'll just set whatever size here because we want
  184.   # to resize the image when we figure out how big the text is.
  185.   $image = Gimp->image_new(64,64,RGB); # don't waste too much  resources ;-/
  186.     
  187.   $tcol = Gimp->palette_get_foreground ();
  188.   Gimp->palette_set_foreground ($fgcolor);
  189.   # Create a layer for the text.
  190.   $text_layer = Gimp->text($image,-1,0,0,$text,10,1,$size,
  191.                           PIXELS,"*",$font,"*","*","*","*"); 
  192.   Gimp->palette_set_foreground ($tcol);
  193.     
  194.   Gimp->layer_set_preserve_trans($text_layer, FALSE);
  195.  
  196.   # Resize the image based on size of text.
  197.   Gimp->image_resize($image,Gimp->drawable_width($text_layer),
  198.                     Gimp->drawable_height($text_layer),0,0);
  199.  
  200.   # create background and merge them
  201.   $bg_layer = Gimp->layer_create ($image,"text",$bgcolor,1);
  202.   Gimp->image_merge_visible_layers ($image,1);
  203.  
  204.   # return
  205.   $image;
  206.   }
  207.  
  208. ##############################################################################
  209. =pod
  210.  
  211. =item C<layer_add_layer_as_mask image,layer,layermask>
  212.  
  213. Take a layer and add it as a mask to another layer, return mask.
  214.  
  215. =cut
  216.  
  217. # [12/23/98] v0.0.1 Tels - First version
  218. sub layer_add_layer_as_mask {
  219.   my ($image,$layer,$layer_mask) = @_;
  220.   my $mask;
  221.  
  222.   Gimp->selection_all ($image);  
  223.   $layer_mask->edit_copy ();  
  224.   Gimp->layer_add_alpha ($layer); 
  225.   $mask = Gimp->layer_create_mask ($layer,0);
  226.   $mask->edit_paste (0);
  227.   Gimp->floating_sel_anchor(Gimp->image_floating_selection($image));
  228.   Gimp->image_add_layer_mask ($image,$layer,$mask);
  229.   $mask;
  230.   }
  231.  
  232. ##############################################################################
  233. # all functions below are by Marc Lehmann
  234. =pod
  235.  
  236. =item C<gimp_text_wh $text,$fontname>
  237.  
  238. returns the width and height of the "$text" of the given font (XLFD format)
  239.  
  240. =cut
  241. sub gimp_text_wh {
  242.    (Gimp->text_get_extents_fontname($_[0],xlfd_size $_[1],$_[1]))[0,1];
  243. }
  244.  
  245. =pod
  246.  
  247. =item C<gimp_image_layertype $alpha>
  248.  
  249. returns the corresponding layer type for an image, alpha controls wether the layer type
  250. is with alpha or not. Example: imagetype: RGB -> RGB_IMAGE (or RGBA_IMAGE).
  251.  
  252. =item C<gimp_layer2imagetype $layertype>
  253.  
  254. returns the corresponding layer type for an image, alpha controls wether the layer type
  255. is with alpha or not. Example: imagetype: RGB -> RGB_IMAGE (or RGBA_IMAGE).
  256.  
  257. =item C<gimp_image_add_new_layer $image,$index,$fill_type,$alpha>
  258.  
  259. creates a new layer and adds it at position $index (default 0) to the
  260. image, after filling it with gimp_drawable_fill $fill_type (default
  261. BG_IMAGE_FILL). If $alpha is non-zero (default 1), the new layer has
  262. alpha.
  263.  
  264. =item C<gimp_image_set_visible $image,@layers>, C<gimp_image_set_invisible $image,@layers>
  265.  
  266. mark the given layers visible (invisible) and all others invisible (visible).
  267.  
  268. =item C<gimp_layer_get_position $layer>
  269.  
  270. return the position the layer has in the image layer stack.
  271.  
  272. =item C<gimp_layer_set_position $layer,$new_index>
  273.  
  274. moves the layer to a new position in the layer stack.
  275.  
  276. =cut
  277. sub gimp_image_layertype {
  278.    my($type,$alpha) = ($_[0]->base_type,$_[1]);
  279.    $type == RGB     ? $alpha ? RGBA_IMAGE     : RGB_IMAGE     :
  280.    $type == GRAY    ? $alpha ? GRAYA_IMAGE    : GRAY_IMAGE    :
  281.    $type == INDEXED ? $alpha ? INDEXEDA_IMAGE : INDEXED_IMAGE :
  282.    die;
  283. }
  284.  
  285. sub gimp_layer2imagetype {
  286.    my $type = shift;
  287.    $type == RGB_IMAGE        ? RGB        :
  288.    $type == RGBA_IMAGE        ? RGB        :
  289.    $type == GRAY_IMAGE        ? GRAY        :
  290.    $type == GRAYA_IMAGE        ? GRAY        :
  291.    $type == INDEXED_IMAGE    ? INDEXED    :
  292.    $type == INDEXEDA_IMAGE    ? INDEXED    :
  293.    die;
  294. }
  295.  
  296. sub gimp_image_add_new_layer {
  297.    my ($image,$index,$filltype,$alpha)=@_;
  298.    my $layer = new Layer ($image, $image->width, $image->height, $image->layertype (defined $alpha ? $alpha : 1), join(":",(caller)[1,2]), 100, NORMAL_MODE);
  299.    $layer->fill (defined $filltype ? $filltype : BG_IMAGE_FILL);
  300.    $image->add_layer ($layer, $index*1);
  301.    $layer;
  302. }
  303.  
  304. sub gimp_image_set_visible {
  305.    my $image = shift;
  306.    my %layers; @layers{map $$_,@_}=(1) x @_;
  307.    for ($image->get_layers) {
  308.       $_->set_visible ($layers{$$_});
  309.    }
  310. }
  311.  
  312. sub gimp_image_set_invisible {
  313.    my $image = shift;
  314.    my %layers; @layers{map $$_,@_}=(1) x @_;
  315.    for ($image->get_layers) {
  316.       $_->set_visible (!$layers{$$_});
  317.    }
  318. }
  319.  
  320. sub gimp_layer_get_position {
  321.    my $layer = shift;
  322.    my @layers = $layer->image->get_layers;
  323.    for (0..$#layers) {
  324.       # the my is necessary for broken perl (return $_ => undef)
  325.       return (my $index=$_) if ${$layers[$_]} == $$layer;
  326.    }
  327.    ();
  328. }
  329.  
  330. sub gimp_layer_set_position {
  331.    my($layer,$new_pos)=@_;
  332.    $pos=$layer->get_position;
  333.    $layer->add_alpha;
  334.    while($pos>$new_pos) {
  335.       $layer->lower_layer;
  336.       $pos--;
  337.    }
  338.    while($pos<$new_pos) {
  339.       $layer->raise_layer;
  340.       $pos++;
  341.    }
  342. }
  343.  
  344. =pod
  345.  
  346. =back
  347.  
  348. =head1 AUTHOR
  349.  
  350. Various, version 1.000 written mainly by Tels (http://bloodgate.com/). The author
  351. of the Gimp-Perl extension (contact him to include new functions) is Marc
  352. Lehmann <pcg@goof.com>
  353.  
  354.